import { createRouter, createWebHistory } from 'vue-router';

// 导入基本页面
import LoginView from '../views/LoginView.vue';
import HomeView from '../views/HomeView.vue';
import ConcertsView from '../views/ConcertsView.vue';
import TicketPurchaseView from '../views/TicketPurchaseView.vue';
import MyTicketsView from '../views/MyTicketsView.vue';
import DetailView from '../views/DetailView.vue';
import VIPCenter from '../views/VIPCenter.vue';
import VipView from '../views/VipView.vue';
import TicketsView from '../views/TicketsView.vue';
// 新增功能组件导入
import ProfileView from '../views/ProfileView.vue';
import TasksView from '../views/TasksView.vue';
import MallView from '../views/MallView.vue';
import TopicsWallView from '../views/TopicsWallView.vue';
import LyricsDanmakuView from '../views/LyricsDanmakuView.vue';
import GuessSongView from '../views/GuessSongView.vue';
import ActivityCalendarView from '../views/ActivityCalendarView.vue';

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/concerts',
    name: 'concerts',
    component: ConcertsView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/tickets/:id',
    name: 'ticketPurchase',
    component: TicketPurchaseView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/my-tickets',
    name: 'myTickets',
    component: MyTicketsView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/detail/:type/:id',
    name: 'detail',
    component: DetailView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/vip',
    name: 'vip',
    component: VIPCenter,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/vip-view',
    name: 'vipView',
    component: VipView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/tickets',
    name: 'tickets',
    component: TicketsView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/albums/:id',
    name: 'albums',
    component: () => import('../views/AlbumsView.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/activities',
    name: 'activities',
    component: () => import('../views/ActivitiesView.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/spin-wheel',
    name: 'spinWheel',
    component: () => import('../views/SpinWheelView.vue'),
    meta: {
      requiresAuth: true
    }
  },
  // 新增功能路由
  {
    path: '/profile',
    name: 'Profile',
    component: ProfileView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/tasks',
    name: 'Tasks',
    component: TasksView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/mall',
    name: 'Mall',
    component: MallView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/topics',
    name: 'TopicsWall',
    component: TopicsWallView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/lyrics-danmaku',
    name: 'LyricsDanmaku',
    component: LyricsDanmakuView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/guess-song',
    name: 'GuessSong',
    component: GuessSongView,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/activity-calendar',
    name: 'ActivityCalendar',
    component: ActivityCalendarView,
    meta: {
      requiresAuth: true
    }
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 简单的路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  
  if (to.meta.requiresAuth) {
    if (!token) {
      // 如果需要认证但没有token，先设置一个模拟token
      localStorage.setItem('token', 'mock-token');
    }
    next();
  } else {
    next();
  }
});

export default router;